package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RequestMapping("/report")
@RestController
public class ReportController {

    @Reference
    MemberService memberService;

    @Reference
    SetmealService setmealService;

    @Reference
    ReportService reportService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            Map<String,Object> map = memberService.getMemberReport();
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }


    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            Map<String,Object> map =  setmealService.getSetmealReport();
            return new Result(true,MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }


    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map<String,Object> map = reportService.getBusinessReportData();
            return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_BUSINESS_REPORT_FAIL);
        }

    }

    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response) {
        //获取运营数据
        try {
            Map<String, Object> map = reportService.getBusinessReportData();
            //构建模板流对象
            InputStream inputStream = this.getClass().getResourceAsStream("/template/report_template.xlsx");
            //创建excel对象
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            //获取工作表对象
            XSSFSheet sheetAt = workbook.getSheetAt(0);
            //获取统计日期
            String reportDate = String.valueOf(map.get("reportDate"));
            //获取第三行
            XSSFRow row = sheetAt.getRow(2);
            //获取第六个单元格
            XSSFCell cell = row.getCell(5);
            cell.setCellValue(reportDate);
//                    todayNewMember :0,新增会员数
            String todayNewMember = String.valueOf(map.get("todayNewMember"));
            //获取第5行
            row = sheetAt.getRow(4);
            //获取第六个单元格
            cell = row.getCell(5);
            cell.setCellValue(todayNewMember);
//                    totalMember :0, 会员总数
            String totalMember = String.valueOf(map.get("totalMember"));
            //获取第5行
            row = sheetAt.getRow(4);
            //获取第8个单元格
            cell = row.getCell(7);
            cell.setCellValue(totalMember);
//                    thisWeekNewMember :0, 本周新会员
            String thisWeekNewMember = String.valueOf(map.get("thisWeekNewMember"));
            //获取第6行
            row = sheetAt.getRow(5);
            //获取第6个单元格
            cell = row.getCell(5);
            cell.setCellValue(thisWeekNewMember);
//                    thisMonthNewMember :0,  本月新会员
            String thisMonthNewMember = String.valueOf(map.get("thisMonthNewMember"));
            //获取第6行
            row = sheetAt.getRow(5);
            //获取第8个单元格
            cell = row.getCell(7);
            cell.setCellValue(thisMonthNewMember);
//                    todayOrderNumber :0,  今天预约数
            String todayOrderNumber = String.valueOf(map.get("todayOrderNumber"));
            //获取第8行
            row = sheetAt.getRow(7);
            //获取第6个单元格
            cell = row.getCell(5);
            cell.setCellValue(todayOrderNumber);
//                    todayVisitsNumber :0, 今天到诊数
            String todayVisitsNumber = String.valueOf(map.get("todayVisitsNumber"));
            //获取第8行
            row = sheetAt.getRow(7);
            //获取第8个单元格
            cell = row.getCell(7);
            cell.setCellValue(todayVisitsNumber);
//                    thisWeekOrderNumber :0, 本周预约数
            String thisWeekOrderNumber = String.valueOf(map.get("thisWeekOrderNumber"));
            //获取第9行
            row = sheetAt.getRow(8);
            //获取第6个单元格
            cell = row.getCell(5);
            cell.setCellValue(thisWeekOrderNumber);
//                    thisWeekVisitsNumber :0,本周到诊数
            String thisWeekVisitsNumber = String.valueOf(map.get("thisWeekVisitsNumber"));
            //获取第8行
            row = sheetAt.getRow(8);
            //获取第8个单元格
            cell = row.getCell(7);
            cell.setCellValue(thisWeekVisitsNumber);
//                    thisMonthOrderNumber :0,本月预约数
            String thisMonthOrderNumber = String.valueOf(map.get("thisMonthOrderNumber"));
            //获取第10行
            row = sheetAt.getRow(9);
            //获取第6个单元格
            cell = row.getCell(5);
            cell.setCellValue(thisMonthOrderNumber);
//                    thisMonthVisitsNumber :0,本月到诊数
            String thisMonthVisitsNumber = String.valueOf(map.get("thisMonthVisitsNumber"));
            //获取第10行
            row = sheetAt.getRow(9);
            //获取第8个单元格
            cell = row.getCell(7);
            cell.setCellValue(thisMonthVisitsNumber);
//获取热门套餐
            List<Map<String, Object>> hotSetmeals = (List<Map<String, Object>>) map.get("hotSetmeal");
            int rowNum = 12;
            for (Map<String, Object> hotSetmeal : hotSetmeals) {
                row = sheetAt.getRow(rowNum);

                cell = row.getCell(4);
                cell.setCellValue(String.valueOf(hotSetmeal.get("name")));
                cell = row.getCell(5);
                cell.setCellValue(String.valueOf(hotSetmeal.get("setmeal_count")));
                cell = row.getCell(6);
                cell.setCellValue(String.valueOf(hotSetmeal.get("proportion")));
                rowNum++;
            }

            // 通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            //设置输入的文件的类型
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=" + "report.xlsx");
            workbook.write(out);
            out.flush();
            out.close();
            workbook.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
return null;

    }
}
